//
//  BaseApp.swift
//  Base
//
//  Created by 元穗 on 2017/5/23.
//  Copyright © 2017年 敬信. All rights reserved.
//

import Extend
import Alamofire

public let kDidConnetNetNoti = "connectNetwork"
public let kDisConnetNetNoti = "disConnectNetwork"

open class BaseApp: AppDelegateEx {
    static let NetListenUrl = "www.baidu.com"

    ///记录网络状态
    public var netStatus = true
    let manager = NetworkReachabilityManager(host: NetListenUrl)
    
    override open func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
        manager!.listener = { status in
            switch status {
            case .notReachable:
                //无网络
                self.netStatus = false
                NotificationCenter.default.post(name: NSNotification.Name(kDisConnetNetNoti), object: nil)
                break
            case .unknown:
                //未知网络
                self.netStatus = false
                NotificationCenter.default.post(name: NSNotification.Name(kDisConnetNetNoti), object: nil)
                break
            case .reachable(.ethernetOrWiFi):
                //wifi
                if (self.netStatus == false) {
                    self.netStatus = true
                    NotificationCenter.default.post(name: NSNotification.Name(kDidConnetNetNoti), object: nil)
                }
                break
            case .reachable(.wwan):
                //蜂窝数据
                if (self.netStatus == false) {
                    self.netStatus = true
                    NotificationCenter.default.post(name: NSNotification.Name(kDidConnetNetNoti), object: nil)
                }
                break
            }
        }
        manager!.startListening()
        
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
    
    open override func applicationWillTerminate(_ application: UIApplication) {
        print("baseApp: 程序被干掉")
    }
}
